<template>
  <div class="tpo-box">
    <div style="cursor: pointer" class="fs-5" @click="router.push('/message')">&lt;</div>
    <div class="fs-5">{{ messageInfo.title }}</div>
    <div>
      <HuUserInfo></HuUserInfo>
    </div>
  </div>
  <div class="messageInfo">
    <div v-if="messageInfo.mid">
      <div v-html="messageInfo.info" class="p-2 fs-6"></div>
      <div class="p-2">
        点击量：{{ messageInfo.hits }} 点赞量：{{ messageInfo.praise }} 回复数：{{
          messageInfo.reply
        }}
      </div>
      <div>
        <NButton @click="showjubao" type="primary">举报</NButton>
      </div>
    </div>
    <div v-else>
      <NEmpty description="评论信息获取中..."></NEmpty>
    </div>
  </div>
  <NDivider>评论</NDivider>
  <div v-if="list.length == 0">
    <NEmpty description="暂无评论"></NEmpty>
    <div class="tc">
      <NButton @click="showAdd" type="primary">抢先发布</NButton>
    </div>
  </div>
  <div v-else>
    <div class="tc p-4">
      <NButton @click="showAdd" type="primary">发布评论</NButton>
    </div>

    <div class="replies">
      <div v-for="d in list" :key="d.mrid">
        <div class="messageInfo">
          <div>
            <NAvatar round :src="Api.userlogo(d.uid)"></NAvatar>
          </div>
          <div>
            <div>
              {{ d.user.info.nickname || d.user.user.username }}
              - <span @click="praiseReply(d)" :class="{ active: d.exInfo.praised }">赞</span>
              <span>{{ d.praise }}</span>
              -<span @click="praiseReply(d)" :class="{ active: d.lastupdate }">发布时间</span>
              <span>{{ Tools.formatDate(d.lastupdate) }}</span>
            </div>
            <div>
              {{ d.info }}
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- 发布评论的对话框 -->
  <div>
    <NModal preset="dialog" v-model:show="visible" title="发布评论">
      <div>
        <NInput type="textarea" v-model:value="info" placeholder="评论内容"></NInput>
      </div>
      <template #action>
        <NButton type="primary" @click="addReply">发布</NButton>
      </template>
    </NModal>
  </div>

  <!-- 举报的对话框 -->
  <div>
    <NModal preset="dialog" v-model:show="avisible" title="举报">
      <div>
        <NInput type="textarea" v-model:value="jbinfo" placeholder="举报内容"></NInput>
      </div>
      <template #action>
        <NButton type="primary" @click="jubao">举报</NButton>
      </template>
    </NModal>
  </div>
</template>

<script setup lang="ts">
import { Api } from '@/types/Api'
import type { BaseDataResult, PageBean, PageVo } from '@/types/BaseResult'
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { NAvatar, useMessage } from 'naive-ui'
import HuUserInfo from '@/components/HuUserInfo.vue'
import { Tools } from '@/types/Tools'
import NEmpty from 'naive-ui/lib/empty/src/empty'
import { NDivider } from 'naive-ui'
import { NButton } from 'naive-ui'
import { NModal, NInput } from 'naive-ui'
import type { TbMessage, TbMessageReply } from '@/types/message'
const message = useMessage()

const router = useRouter()
const route = useRoute()

const mid = route.params.id
console.log('留言mid：', mid)

const loading = ref(true)

// #region 留言详情
const messageInfo = ref({} as TbMessage)

const query = () => {
  loading.value = true
  Api.get(`/message/${mid}`, {}, (data: BaseDataResult<TbMessage>) => {
    loading.value = false
    if (data.success) {
      messageInfo.value = data.data
      queryReply()
      return
    }
    message.error(data.message)
  })
}
// #endregion

// #region 留言评论
const page = ref({ pageNumber: 1, pageSize: 10 } as PageBean)
const list = ref([] as Array<TbMessageReply>)

const queryReply = () => {
  loading.value = true
  Api.get(
    `/message/reply/${mid}/${page.value.pageSize}/${page.value.pageNumber}/1`,
    {},
    (data: BaseDataResult<PageVo<TbMessageReply>>) => {
      loading.value = false
      if (data.success) {
        list.value = data.data.list
        page.value = data.data.pageBean
        return
      }
      message.error(data.message)
    },
  )
}

const praiseReply = (info: TbMessageReply) => {
  Api.post(`/reply/praise/${info.mrid}`, {}, (data: BaseDataResult<number>) => {
    if (data.success) {
      info.exInfo.praised = !info.exInfo.praised
      return
    }
    message.error(data.message)
  })
}
// #endregion

// #region 发布评论
const visible = ref(false)
const info = ref('')

const showAdd = () => {
  info.value = ''
  visible.value = true
}

const addReply = () => {
  loading.value = true
  Api.post(
    `/reply`,
    {
      mid: mid,
      info: info.value,
    },
    (data: BaseDataResult<TbMessageReply>) => {
      loading.value = false
      if (data.success) {
        visible.value = false
        queryReply()
        return
      }
      message.error(data.message)
    },
  )
}

//#endregion

// #region 举报
const avisible = ref(false)
const jbinfo = ref('')

const showjubao = () => {
  jbinfo.value = ''
  avisible.value = true
}
const jubao = () => {
  loading.value = true
  Api.post(
    `/report/message`,
    {
      mid: mid,
      info: jbinfo.value,
    },
    (data: BaseDataResult<TbMessageReply>) => {
      loading.value = false
      if (data.success) {
        visible.value = false
        return
      }
      message.error(data.message)
    },
  )
}
// #endregion

onMounted(() => {
  query()
})
</script>
<style scoped>
.tpo-box {
  display: flex;
  padding: 1rem;
  justify-content: space-between;
}

.message {
  width: 50vw;
  margin: 0px auto;
  :deep(p) {
    font-size: 1.2rem;
    text-indent: 2em;
  }

  > div:first-of-type {
    font-size: 1.2rem;
  }
}

.replies {
  width: 50vw;
  margin: 0px auto;
  > div {
    margin-bottom: 0.5rem;
  }
}

.messageInfo {
  width: 60vw;
  margin: 0px auto;
  :deep(img) {
    width: 50vw;
    margin: 0px auto;
  }
}
</style>
